clear all
set more off
set seed 413202


*******************************************************************
//// 12-BUILD_MAIN: CONSTRUCT MASTER ANALYSIS DATASET ////
*******************************************************************



*************************************
//// SET SAMPLE PARAMETERS HERE ////
*************************************

local FYEAR = 2007
local LYEAR = 2016
local MINSTOPS = 50


******************************
//// READ IN STOPS DATA ////
******************************

use "${out}/2-stopsfhp", clear



******************************
//// SAMPLE RESTRICTIONS ////
******************************

keep if speedsample==1
keep if dlvalid==1 
keep if !mi(officerid)
drop if companion==1

keep if year>=`FYEAR' & year<=`LYEAR' 

qui compress
saveold "${temp}/TEMP", replace




*************************
//// SIMPLE LENIENCE ////
*************************

* SPEEDING STOPS *
keep if speedsample == 1 
keep if speeddiff >= 9 
gen discount = (speeddiff==9)
label var discount "Discount"
gen harsh = (speeddiff>=10)
label var harsh "Harsh Fine"

* SIMPLE LO MEASURE *
bysort officerid: egen sumstops = sum(1)
bysort officerid: egen sumharsh = sum(harsh)
gen lo = (sumharsh - harsh)/(sumstops - 1)
replace lo = . if sumstops < 50
label var lo "Leave-Out Harsh Rate"
keep if !mi(lo) 
gen Z = lo
label var Z "Stringency"

drop sumstops sumharsh 
 


***********************
//// FIXED EFFECTS ////
***********************

*egen citationid = group(citationnumber countynum)
egen driverid = group(dlnumber)

label var citationid "Numeric Citation ID"
label var driverid "Numeric Driver ID"
order citationid driverid

egen totfe = group(countynum highway year month wknd shift)
label var totfe "Location-Time FE"

egen gpsfe = group(roadid countynum highway year month wknd shift)
label var gpsfe "GPS Location-Time FE"



***********************
//// FINAL SAMPLE ////
***********************

bysort officerid: egen Nstops = sum(1)
keep if Nstops >= `MINSTOPS'
drop Nstops

#delimit ;
global COV = "female age agesq age_miss race_b race_h race_o race_u 
priorprison local logzip zipincome_miss logprice veh_miss 
speed_py1 other_py1 crashany_py1" ;
#delimit cr

reghdfe speed_ny1 Z $COV, absorb(totfe)
keep if e(sample)==1


************************
/// COVARIATE CELLS ////
************************

/// Broader cells ------------
gen     temprace = race
replace temprace = "OU" if race=="O"|race=="U"
summ age, d
gen old = age>r(p50)
egen covbin1 = group(female old temprace cite_py1)

/// Tighter Cells -------------
binscatter harsh age,       nodraw nq(4) gen(agebin)
binscatter harsh zipincome, nodraw nq(4) gen(zipbin)
egen covbin2 = group(female temprace agebin zipbin cite_py1)

label var covbin1 "Covariate Cell (Broader)"
label var covbin2 "Covariate Cell (Narrower)"
drop old temprace agebin zipbin 


*******************
//// CLEAN UP ////
*******************

drop crash-speedsample instate dlvalid lo
order citationid-lenientpart discount harsh Z totfe gpsfe covbin1 covbin2


****************
//// STORE ////
****************

qui compress
saveold "${out}/4-main", replace
rm "${temp}/TEMP.dta"







